angular.module('ProduceWork').controller('MOCompleteRptController', ['$scope', 'PublicService', 'PublicModule', 'PublicEnum', 'PublicFunction', 'BarcodeEnum', '$location', '$modal', 'Authentication',
	function($scope, PublicService, PublicModule, PublicEnum, PublicFunction, BarcodeEnum, $location, $modal, Authentication) {

		PublicModule.initModule($scope, 'RcvRecord');
		$scope.Module.RcvRecord.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
		$scope.Module.RcvRecord.QueryParameter.Populate = [{
			path: 'BarcodeMaster',
			select: 'Barcode MOLotNumber ShipLotNumber'
		}, {
			path: 'ItemMaster',
			select: 'Name Specification'
		}, {
			path: 'RcvUser',
			select: 'Name',
			model: 'User',
			populate: {
				path: 'Department',
				select: 'Name',
				model: 'Department'
			}
		}, {
			path: 'ConnectArea',
			select: 'Name'
		}];

		//日期
		var curday = calCurrentMonthTime(true, 0);
		var lastweekstart = calCurrentMonthTime(true, 1) + ' 00:00:00';
		var lastweekend = calCurrentMonthTime(false, 1) + ' 23:59:59';
		var curweekstart = calCurrentMonthTime(true, 2) + ' 00:00:00';
		var curweekend = calCurrentMonthTime(false, 2) + ' 23:59:59';
		var lastmonthstart = calCurrentMonthTime(true, 3) + ' 00:00:00';
		var lastmonthend = calCurrentMonthTime(false, 3) + ' 23:59:59';
		var curmonthstart = calCurrentMonthTime(true, 4) + ' 00:00:00';
		var curmonthend = calCurrentMonthTime(false, 4) + ' 23:59:59';

		var condition = {
			RcvBusinessType: 1,
		};

		$scope.Module.RcvRecord.PageSize = 15;
		$scope.Module.RcvRecord.load(condition);

		$scope.ProductComplete = true;
		$scope.ConnectArea = false;
		$scope.All = false;
		$scope.Day = false;
		$scope.LastWeek = false;
		$scope.CurrentWeek = true;
		$scope.LastMonth = false;
		$scope.CurrentMonth = false;
		$scope.AllDay = false;


		//成品完工
		$scope.cbRcvRpt = function() {
			if ($scope.ProductComplete) {
				$scope.ConnectArea = false;
				$scope.All = false;
				$scope.$digest();
			};
		};

		//交接区入库
		$scope.cbConnect = function() {
			if ($scope.ConnectArea) {
				$scope.ProductComplete = false;
				$scope.All = false;
				$scope.$digest();
			};
		};

		//全部类型
		$scope.cbRcvAll = function() {
			if ($scope.All) {
				$scope.ProductComplete = false;
				$scope.ConnectArea = false;
				$scope.$digest();
			};
		};

		//当天
		$scope.cbCurrentDay = function() {
			if ($scope.Day) {
				$scope.LastWeek = false;
				$scope.CurrentWeek = false;
				$scope.LastMonth = false;
				$scope.CurrentMonth = false;
				$scope.AllDay = false;
				$scope.$digest();
			};
		};

		//上周
		$scope.cbLastWeek = function() {
			if ($scope.LastWeek) {
				$scope.Day = false;
				$scope.CurrentWeek = false;
				$scope.LastMonth = false;
				$scope.CurrentMonth = false;
				$scope.AllDay = false;
				$scope.$digest();
			};
		};

		//本周
		$scope.cbCurrentWeek = function() {
			if ($scope.CurrentWeek) {
				$scope.Day = false;
				$scope.LastWeek = false;
				$scope.LastMonth = false;
				$scope.CurrentMonth = false;
				$scope.AllDay = false;
				$scope.$digest();
			};
		};

		//上月
		$scope.cbLastMonth = function() {
			if ($scope.LastMonth) {
				$scope.Day = false;
				$scope.LastWeek = false;
				$scope.CurrentWeek = false;
				$scope.CurrentMonth = false;
				$scope.AllDay = false;
				$scope.$digest();
			};
		};

		//本月
		$scope.cbCurrentMonth = function() {
			if ($scope.CurrentMonth) {
				$scope.Day = false;
				$scope.LastWeek = false;
				$scope.CurrentWeek = false;
				$scope.LastMonth = false;
				$scope.AllDay = false;
				$scope.$digest();
			};
		};

		//全部时间
		$scope.cbAllDate = function() {
			if ($scope.AllDay) {
				$scope.Day = false;
				$scope.LastWeek = false;
				$scope.CurrentWeek = false;
				$scope.LastMonth = false;
				$scope.CurrentMonth = false;
				$scope.$digest();
			};
		};

		//同步ERP
		$scope.btnSyncERPComplete = function() {
			var rcds = new Array();
			for (var i in $scope.Module.RcvRecord.Records) {
				if ($scope.Module.RcvRecord.Records[i].IsSelected) {
					rcds.pushs($scope.Module.RcvRecord.Records[i]);
				};
			};
			if (rcds.length < 1) {
				PublicFunction.alert("请先勾选记录再执行该操作！", "操作有误");
			} else {

			};
		}

		//计算起止时间
		function calCurrentMonthTime(isStartTime, dateType) { //isStartTime-判断起止，0-当天，1-上周，2-本周，3-上月，4-本月，5-全部
			var now = new Date(); //当前日期 
			var nowDayOfWeek = now.getDay(); //今天本周的第几天
			var nowDay = now.getDate(); //当前日
			var nowMonth = now.getMonth(); //当前月 
			var nowYear = now.getYear(); //当前年 
			nowYear += (nowYear < 2000) ? 1900 : 0; //
			var lastMonthDate = new Date(); //上月日期
			lastMonthDate.setDate(1);
			lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
			var lastYear = lastMonthDate.getYear();
			var lastMonth = lastMonthDate.getMonth();

			var rtnTime;
			if (dateType == 0) { //当天
				if (isStartTime) {
					var getCurrentDate = new Date(nowYear, nowMonth, nowDay);
					rtnTime = formatDate(getCurrentDate)
				} else {

				};
			} else if (dateType == 1) { //上周
				if (isStartTime) {
					var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7 + 1);
					rtnTime = formatDate(getUpWeekStartDate);
				} else {
					var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 7 + 1));
					rtnTime = formatDate(getUpWeekEndDate);
				};
			} else if (dateType == 2) { //本周
				if (isStartTime) {
					var getWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
					rtnTime = formatDate(getWeekStartDate);
				} else {
					var getWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek + 1));
					rtnTime = formatDate(getWeekEndDate);
				};
			} else if (dateType == 3) { //上月
				if (isStartTime) {
					var getLastMonthStartDate = new Date(nowYear, lastMonth, 1);
					rtnTime = formatDate(getLastMonthStartDate);
				} else {
					var getLastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(nowYear, lastMonth));
					rtnTime = formatDate(getLastMonthEndDate);
				};
			} else if (dateType == 4) { //本月
				if (isStartTime) {
					var getMonthStartDate = new Date(nowYear, nowMonth, 1);
					rtnTime = formatDate(getMonthStartDate);
				} else {
					var getMonthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear, nowMonth));
					rtnTime = formatDate(getMonthEndDate);
				};
			} else if (dateType == 5) { //全部
				if (isStartTime) {

				} else {

				};
			};

			return rtnTime;
		}

		//获得某月的天数
		function getMonthDays(nowYear, myMonth) {
			var monthStartDate = new Date(nowYear, myMonth, 1);
			var monthEndDate = new Date(nowYear, myMonth + 1, 1);
			var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
			return days;
		}

		//格式化时间
		function formatDate(date) {
			var myyear = date.getFullYear();
			var mymonth = date.getMonth() + 1;
			var myweekday = date.getDate();
			return (myyear + "\\" + mymonth + "\\" + myweekday);
		}
	}
]);